﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using StarLabel.label.item;
namespace StarLabel.label
{
    /// <summary>
    /// Copyright 2019  ZhengJi. All Rights Reserved.
    /// 标签定义root 
    /// </summary>
    public class Label
    {
        public string Width { get; set; }
        public string Height { get; set; }
        /// <summary>
        /// 标签垂直之间空隙，单位：MM
        /// </summary>
        public string GapVertical { get; set; }

        /// <summary>
        /// 标签横向之间空隙，单位：MM
        /// </summary>
        public string GapHorizontal { get; set; }

        /// <summary>
        /// 边界位移，单位:MM
        /// </summary>
        public string OffsetLength { get; set; }


       public string UnitType { get; set; }

        public string Direction { get; set; }
        public int LabelsPerRow { get; set; }
        public ArrayList Items { get; set; }

        

  
        public Label()
        {
            this.Items = new ArrayList();
        }

        public static Label parseFromTemplate(string templateFile )
        {
            string outPut = string.Empty;
            Label lblRet = new Label();
            try
            {
               
                XmlDocument doc = new XmlDocument();
                doc.Load(templateFile);

                XmlNodeList nodes = doc.SelectSingleNode("StarLabel").ChildNodes;

              
                lblRet.Width = doc.SelectSingleNode("StarLabel").Attributes["Width"].Value;              
                lblRet.Height = doc.SelectSingleNode("StarLabel").Attributes["Height"].Value;
                lblRet.OffsetLength = doc.SelectSingleNode("StarLabel").Attributes["OffsetLength"].Value;
                lblRet.GapVertical = doc.SelectSingleNode("StarLabel").Attributes["GapVertical"].Value;
                lblRet.GapHorizontal = doc.SelectSingleNode("StarLabel").Attributes["GapHorizontal"].Value;
                lblRet.UnitType =doc.SelectSingleNode("StarLabel").Attributes["UnitType"].Value;
                lblRet.Direction = doc.SelectSingleNode("StarLabel").Attributes["Direction"].Value;
                lblRet.LabelsPerRow =int.Parse( doc.SelectSingleNode("StarLabel").Attributes["LabelsPerRow"].Value);
                foreach (XmlElement node in nodes)
                {
                    if (node.Name == "Items")
                    {
                        XmlNodeList nodesc = node.ChildNodes;

                        foreach (XmlNode nd in nodesc)
                        {


                            if (!(nd is XmlElement)) continue;

                            
                           Item it= ItemManager.CreateItem((XmlElement)nd);

                            lblRet.Items.Add(it);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return lblRet;
        }

       
    }
}
